RealDataGrid = {};
RealDataGrid.selectDeviceIds = ""; //需要刷新的设备id
//表格当前选中的实时数据的设备Id及车牌号
RealDataGrid.selecteddvcId = 0;
RealDataGrid.selecteddvcSim = '';
RealDataGrid.realDataList = []; //实时数据加载到内存中，进行内存分页
RealDataGrid.rowIndexMap = {}; //根据实时数据ID,找到内存数组realDataList的索引

//点击数据表格的行时，显示地图设备
RealDataGrid.onClickRow = function (rowIndex, rowData) {
	var simNo = rowData.simNo;
	var dvcId = rowData.dvcId;
	MyMap.showVehicleOnMap(dvcId);
	RealDataGrid.selecteddvcId = dvcId;
	RealDataGrid.selecteddvcSim = simNo;
}
/**
 * 当用户点击设备树的时候，设置车牌号和Id
 */
RealDataGrid.setdvcSim = function (dvcSim, dvcId) {
	this.selecteddvcSim = dvcSim;
	this.selecteddvcId = dvcId;
}


function pagerFilter(data) {
	if (typeof data.length == 'number' && typeof data.splice == 'function') {	// is array
		data = {
			total: data.length,
			rows: data
		}
	}
	var dg = $(this);
	var opts = dg.datagrid('options');
	var pager = dg.datagrid('getPager');
	pager.pagination({
		onSelectPage: function (pageNum, pageSize) {
			opts.pageNumber = pageNum;
			opts.pageSize = pageSize;
			pager.pagination('refresh', {
				pageNumber: pageNum,
				pageSize: pageSize
			});
			dg.datagrid('loadData', data);
		}
	});
	if (!data.originalRows) {
		data.originalRows = (data.rows);
	}
	var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
	var end = start + parseInt(opts.pageSize);
	data.rows = (data.originalRows.slice(start, end));
	return data;
}

/**
 * 定时发送ajax请求，更新表格,
 * 如有表格中有对应的设备就更新
 * 如果没有该设备，就增加一套
 */
RealDataGrid.refresh = function (update) {
	if (!this.selectDeviceIds || this.selectDeviceIds.length == 0)
		return;
	var me = this;
	var url = path + "/realData/refreshRealData";
	var params = {update: update};
	$.ajax({
		type: "POST",
		url: url,
		data: params,
		error: function () {
			//alert("网络连接错误，无法读取数据!");
			//Utility.done();
		},
		success: function (data) {
			//var gridData = {Rows:data};

			//登录
			if (data.logoutFlag) {
				window.top.location = path + "/login";
				return false;
			}

			var records = data.rows;
			if (records == null)
				return;
			//var start = new Date();
			if (update == false) {
				//清除地图上已有的设备图标，重新生成地图图标
				MyMap.clearAllVehicle();
				me.rowIndexMap = {};
				//第一次全部加载
				me.realDataList = records; //保存在内存中，进行分页
				for (var m = 0; m < records.length; m++) {
					var record = records[m];
					me.rowIndexMap[record.simNo] = m;
				}


			} else {
				//没有要更新的实时数据
				if (records.length == 0)
					return;
				//第一次全部加载后，转入更新模式，即只从后台查询到有更新的实时数据，减少流量
				for (var m = 0; m < records.length; m++) {
					var record = records[m];
					var index = me.rowIndexMap[record.simNo];
					if (index != null) {
						me.realDataList[index] = record; //更新内存记录
					}
				}
			}

			//me.realDataGrid.datagrid("loadData",me.realDataList);

			MyMap.createMarkerByGpsData_1(records);

		}
	});


}

function formatOnlineState(val, row) {

	if (val) {
		var onlineImage = path + "/images/power_on.png";
		return '<img src="' + onlineImage + '" />';
	} else {
		var offlineImage = path + "/images/power_off.gif";
		return '<img src="' + offlineImage + '" />';
	}
}


RealDataGrid.create = function () {
	this.update = false;
/*	var treeUrl = path + "/realData/refreshRealData";
	//绑定数据列表 //align: 'left' 控制对齐方式
	this.realDataGrid = $("#realDataGrid");
	this.realDataGrid.datagrid({
		columns: [[
			{field: 'simNo', title: '设备号', width: 120, sortable: false},
			{field: 'depName', title: '所属组', width: 120, sortable: false},
			{field: 'sendTime', title: '定位时间', width: 120, sortable: false},
			{field: 'longitude', title: '经度', width: 120, sortable: false},
			{field: 'latitude', title: '纬度', width: 120, sortable: false},
			{field: 'velocity', title: '速度', width: 40, sortable: false},
			{field: 'directionDescr', title: '方向', width: 80, sortable: false},
			{field: 'location', title: '位置', width: 120, sortable: false},
		]],
		fit: true,
		//toolbar: "#commandToolbar",
		url: treeUrl,
		method: 'POST',
		idField: 'simNo',
		striped: true,
		fitColumns: true,
		singleSelect: true,
		rownumbers: true,
		pagination: true,
		nowrap: true,
		//loadFilter: pagerFilter,
		pageSize: 100,
		pageList: [50, 100, 150, 200],
		showFooter: true,
		onClickRow: function (rowIndex, rowData) {
			me.onClickRow(rowIndex, rowData);
		}
	});*/
	this.refreshInterval = 30;
	this.timerName = "realdataTimer";
	var strInterval = this.refreshInterval + 's';
	var me = this;
	$('body').everyTime(strInterval, this.timerName, function () {
		//do something...
		me.refresh(true); //更新模式
	});

	//return this.realDataGrid;

}

